package com.o2o.cleaning.month.platform.ebusiness_plat.jumei_2019_7

import com.alibaba.fastjson.{JSON, JSONObject}
import com.mongodb.spark.MongoSpark
import com.mongodb.spark.config.ReadConfig
import com.mongodb.spark.rdd.MongoRDD
import com.o2o.utils.Iargs
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{DataFrame, SparkSession}
import org.bson.Document

/**
  * @ Auther: o2o-rd-0008
  * @ Date:   2020/5/31 15:17
  * @ Param:  ${PARAM}
  * @ Description: 
  */
object MongoDBData {



  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .appName(s"${this.getClass.getSimpleName}")
      .config("spark.debug.maxToStringFields", "2000")
      .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.caseSensitive", "true")
      .master("local[*]")
      .getOrCreate()

    val sc = spark.sparkContext
    sc.hadoopConfiguration.set("fs.s3a.access.key", Iargs.OBSACCESS)
    sc.hadoopConfiguration.set("fs.s3a.secret.key", Iargs.OBSSECRET)
    sc.hadoopConfiguration.set("fs.s3a.endpoint", Iargs.OBSENDPOINT)
    sc.setLogLevel("WARN")


    /** *******************重要重要重要重要重要重要 ************************************************************/
    val year = 2021
    val month = 6
    val timeStamp = "1624982400"
    val obs = "s3a://"
    val platform = "Jumei"
    val readUri = "mongodb://root:O2Odata123!@ 192.168.0.149:27017/admin"

    val readDatabase = "Jumei"
    val readCollection = s"jumei_210${month}"
    //拉取数据保存路径
//    val resultUrl = s"s3a://o2o-sourcedata-${year}/obs-source-${year}/620/${platform}/"
//    val resultUrl = s"s3a://o2o-sourcedata-${year}/obs-source-${year}/615/${platform}/"
    val resultUrl = s"s3a://o2o-sourcedata-${year}/obs-source-${year}/${month}/${platform}/"
//    val resultUrl = s"s3a://o2o-dataproces-group/zyf/${year}/${month}/jumei/"
    // 原有分类路径
    val cate_path = "s3a://o2o-dimension-table/category_table/categoryFile_tao/jumei_2019_9"
    //保存本次新增分类保存路径
    val cate_newAdd_path = s"s3a://o2o-dimension-table/category_table/categoryFile_tao/jumei_cate_newAdd_${year}_${month}"

    // 读取MongoDB数据
    val dataRDD: RDD[String] = loadMongoData(spark, readUri, readDatabase, readCollection, timeStamp)

    val dataFrame: DataFrame = spark.read.json(dataRDD)

    val newCateframe: DataFrame = dataFrame.selectExpr("subCategoryId").except(spark.read.json(cate_path).selectExpr("subCategoryId")).dropDuplicates("subCategoryId")
      .join(dataFrame.selectExpr("rootCategoryId", "rootCategoryName", "categoryId", "categoryName", "subCategoryId", "subCategoryName"), Seq("subCategoryId"), "left")
    println("新增分类count::::"+newCateframe.count())
    if (newCateframe.collect().length > 0) newCateframe.repartition(1).write.json(cate_newAdd_path)

    dataFrame.repartition(10).write.json(resultUrl)

  }
  def loadMongoData(spark: SparkSession, readUri: String, readDatabase: String, readCollection: String, timeStamp: String): RDD[String] = {

    val readConfig = ReadConfig(Map("uri" -> readUri, "database" -> readDatabase, "collection" -> readCollection))

    val mongoRDD: MongoRDD[Document] = MongoSpark.load(spark.sparkContext, readConfig)

    val rdd: RDD[String] = mongoRDD.map(line => {
      val nObject: JSONObject = JSON.parseObject(line.toJson())
      nObject.remove("_id")
      nObject.toString
    })

    rdd
  }
}
